package com.example.ecommercemarketing.handler.impl;

import com.example.ecommercemarketing.dto.MarketingElementsDTO;
import com.example.ecommercemarketing.dto.ProductDTO;
import com.example.ecommercemarketing.dto.UserDTO;
import com.example.ecommercemarketing.handler.MarketingElementsHandler;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 通过付费商家获取营销素材
 */
public class PaidMerchantHandler implements MarketingElementsHandler {

    @Override
    public MarketingElementsDTO handle(ProductDTO product, UserDTO user) {
        if (!product.isPaidMerchant()) {
            return null;
        }

        //这里直接写死了一些测试数据，实际项目中是通过发其它应用的接口、查数据库、经过一些繁琐的业务逻辑处理来获取数据的
        MarketingElementsDTO elements = new MarketingElementsDTO();
        elements.setRecommendText("【商家赞助】" + product.getName() + "，品质保证！");
        elements.setProductHighlights("商家特别推荐，品质有保障");
        elements.setMarketingImageUrl("https://example.com/marketing/merchant/" + product.getMerchantId() + "/" + product.getId() + ".jpg");
        elements.setExtraInfo("商家付费推广");
        elements.setSource("paid_merchant");

        Random random = new Random();
        int randomTime = random.nextInt(15);
        //模拟实际业务中的耗时
        try {
            TimeUnit.MILLISECONDS.sleep(randomTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return elements;
    }
}